package couk.mmtdigital.sm.control.commands
{
	import couk.mmtdigital.sm.ApplicationFacade;
	import couk.mmtdigital.sm.model.FileListProxy;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	public class NewFolderCommand extends SimpleCommand implements ICommand
	{
		
		override public function execute(notification:INotification):void{
			mkDir(notification.getBody() as String);
		}
		
		private function mkDir(dir:String):void{
			var fileRO:RemoteObject = new RemoteObject("spreadsheetServices");
			var fileListProxy:FileListProxy = facade.retrieveProxy(FileListProxy.NAME) as FileListProxy;
			fileRO.source = "sm.FileList";
			fileRO.addEventListener(FaultEvent.FAULT, faultHandler);
			fileRO.makeDir.addEventListener(ResultEvent.RESULT, resultHandler);
			fileRO.makeDir(fileListProxy.currentFile+"/"+dir);
		}
		
		//Handler to catch failed connect to the gateway
		private function faultHandler(fault:FaultEvent):void
        {
        	trace("code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail);
        }
        
        
        private function resultHandler(evt:ResultEvent):void{
        	if(evt.result['valid'] == 1){
        		sendNotification(ApplicationFacade.FILELIST);
        	}
        }
	}
}